package eece261.dChatter;

import java.net.InetAddress;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class JoinGroup extends Activity {
	
	// For logging.
	private static final String TAG = "JoinGroup";
	
	private static final String default_ip_address_ = "228.6.8.7";
	private static final int    default_port_       = 6759;
	
	private String			  username_;
	
	private EditText          edit_ip_;
	private EditText          edit_port_;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		
		Log.d(TAG, "JoinGroup created.");
        
		super.onCreate(savedInstanceState);
        setContentView(R.layout.joingroup);
        
        edit_ip_    = (EditText)findViewById(R.id.IPEdit);    
        edit_port_ 	= (EditText)findViewById(R.id.PortEdit);
        
        edit_ip_.setText(default_ip_address_);
        edit_port_.setText(String.valueOf(default_port_));
        
        final Button joinButton = (Button)findViewById(R.id.JoinGroupButton);
        joinButton.setOnClickListener(new View.OnClickListener() {
        	
            public void onClick(View v) {
            	
            	String ip = edit_ip_.getText().toString();
            	String port = edit_port_.getText().toString();
            	
            	if(validIPPort(ip,port))
            	{
            		InetAddress ip_inet = null;
            		try {
            			ip_inet = InetAddress.getByName(ip);
            		} catch (UnknownHostException e) {
            			e.printStackTrace();
            		}

            		if (ip_inet != null && ip_inet.isMulticastAddress())
            		{
            			int port_int = Integer.parseInt(port);
            			Intent intent = new Intent(JoinGroup.this, ChatWindow.class);
            			intent.putExtra("ip", ip);
            			intent.putExtra("port", port_int);
            			intent.putExtra("username", username_);
            			startActivity(intent);
            		}
            	}
            }
        });
        
        Bundle extras = getIntent().getExtras();
        if (extras !=null)
        	username_ = extras.getString("username");
	}
	
    @Override
    protected void onStart() {
    	super.onStart();
    	
    }
    
	public boolean validIPPort(String IP, String Port)
	{
		int temp = Integer.parseInt(Port);
		//if(temp<49152 || temp>65535)
		if(temp<0 || temp>65535)
		{
			return false;
		}
		int count = 1;
		String t1 = "";
		char t9 = ' ';
		for(int ii=0;ii<IP.length();ii++)
		{
			t9 = IP.charAt(ii);
			int t8 = (int)(t9-'0');
			
			if((t8<10 && t8 >0) || t9 == '.')
			{
				if(t9 != '.')
				{
					t1 = t1 + t9;
				}
				else
				{
					count = count + 1;
					if(count>4)
					{return false;
					}
					int kk = Integer.parseInt(t1);
					if(kk<0 || kk>255)
					{
						return false;
					}
					t1 = "";
				}
			}
			else
			{
				return false;
			}
		}
		int kk = Integer.parseInt(t1);
		if(kk<0 || kk>255)
		{
			return false;
		}
		if(count==4)
		{
			return true;
		}

		return false;
	}
}